package com.googlecode.dgwt.rebind.feature;

import com.google.gwt.core.ext.Generator;
import com.google.gwt.core.ext.GeneratorContext;
import com.google.gwt.core.ext.TreeLogger;
import com.google.gwt.core.ext.UnableToCompleteException;
import com.google.gwt.core.ext.typeinfo.JClassType;
import com.google.gwt.core.ext.typeinfo.NotFoundException;
import com.google.gwt.core.ext.typeinfo.TypeOracle;


/**
 * @author      Wellington Tadeu dos Santos
 * @since       DGWT v0.01
 */
public class FeatureGenerator extends Generator {

    
    @Override
    public String generate(TreeLogger logger, GeneratorContext context, String typeName) throws UnableToCompleteException {
        
        TypeOracle typeOracle = context.getTypeOracle();
        JClassType targetClass;

        try {
            targetClass = typeOracle.getType(typeName);
        } catch (NotFoundException e) {
            logger.log(TreeLogger.ERROR, "No such type", e);
            throw new UnableToCompleteException();
        }
        
         
        if (typeOracle.findType(FeatureSourceCreator.FEATURE_CLASSNAME).isAssignableFrom(targetClass)) {
            return new FeatureSourceCreator(context, logger, typeOracle).emitClass(targetClass);
        }
        
        return new FeatureClassTypeSourceCreator(context, logger, typeOracle).emitClass(targetClass);
       
    }

    

}
